package day05

import "math"

// day05
//
// 1035. 不相交的线
//
// https://leetcode.cn/problems/uncrossed-lines/
//
// ```go
func maxUncrossedLines(nums1 []int, nums2 []int) int {
	m, n := len(nums1), len(nums2)
	dp := make([][]int, m+1)
	for i := range dp {
		dp[i] = make([]int, n+1)
	}
	ans := 0
	for i := 1; i <= m; i++ {
		for j := 1; j <= n; j++ {
			if nums1[i-1] == nums2[j-1] {
				dp[i][j] = dp[i-1][j-1] + 1
			} else {
				dp[i][j] = max(dp[i-1][j], dp[i][j-1])
			}
			ans = max(ans, dp[i][j])
		}
	}
	return ans
}

//```

// 53. 最大子数组和
//
// https://leetcode.cn/problems/maximum-subarray/
//
// ```go
func maxSubArray(nums []int) int {
	n := len(nums)
	dp := make([]int, n+1)
	ans := math.MinInt32
	for i := 1; i <= n; i++ {
		dp[i] = max(dp[i-1]+nums[i-1], nums[i-1])
		ans = max(ans, dp[i])
	}
	return ans
}

//```

// 392. 判断子序列
//
// https://leetcode.cn/problems/is-subsequence/
//
// ```go
func isSubsequence(s string, t string) bool {
	m, n := len(s), len(t)
	dp := make([][]int, m+1)
	for i := range dp {
		dp[i] = make([]int, n+1)
	}
	ans := 0
	for i := 1; i <= m; i++ {
		for j := 1; j <= n; j++ {
			if s[i-1] == t[j-1] {
				dp[i][j] = dp[i-1][j-1] + 1
			} else {
				dp[i][j] = max(dp[i-1][j], dp[i][j-1])
			}
			ans = max(ans, dp[i][j])
		}
	}
	return ans == m
}

//```
